package com.crayon2f.test;

import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by feifan.gou@gmail.com on 2025/3/20 10:29.
 */
public class TEST2025_03 {


    String[] appIds = {"ecb-monitor","ecb-mes-tpm","qd-boiler-devops-ykd","trade-server-api-monitor","uds-index-formula-manager"};

    @Test
    public void getDevOps() throws JsonProcessingException {

        List<String> list = Lists.newArrayList();
        for (String appId : appIds) {
            String url = "https://tenant-management-service-com.ennew.com/tenant-management/app/list/condition?pageSize=1&pageNumber=1&code=" + appId;
            String body = HttpUtil.createGet(url).header("ennUnifiedAuthorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJVbmlmeUF1dGhTU08iLCJoYXNUZW5hbnQiOnRydWUsImxvZ2luVHlwZSI6IjEwMDAiLCJpc3MiOiIxMzM4ODI2MzA1MjEyMTc0Mzc1IiwidGVybWluYWxUeXBlIjoiUEMtV0VCLUZJUkVGT1giLCJhdWQiOiJhdWQiLCJoYXNSZWxhdGlvbnNoaXBUZW5hbnQiOmZhbHNlLCJuYmYiOjE3NDI0Mzc0NTIsImFwcElkIjoidGVuYW50LW1hbmFnZW1lbnQtc2VydmljZSIsIm5pY2tuYW1lIjoi5Yu-6Z2e5YehIiwidGVuYW50SWQiOiIxMzg3MzMwNjAyOTQ0Njc1ODQyIiwiZ3JhbnRjb2RlIjoiTVRNek9EZ3lOak13TlRJeE1qRTNORE0zTlNOUVF5MVhSVUl0UmtsU1JVWlBXQ00zWTJJNU5tUTNZalF4Tm1ObU9ETXpPRFZsTXpBeVpHTTRaamM0TnpZMlpRIiwiZXhwIjoxNzQyNTIzODUyLCJpYXQiOjE3NDI0Mzc0NTIsImp0aSI6IjUyM2RmMDJjLTdhZTYtNDFkYi1iN2EyLTZkOTIwY2E2YjYzZSIsInVzZXJuYW1lIjoiR09VRkVJRkFOIn0.WDvaBfkuHuLrIyRyBc2ETqtp1NrGOjJkehehX6Oz7T62fXue2zm42J6dZ4D-NIxRy7yGQIm2yzqHG9jRjL8ZxPbMTejhT48W0lhuTxpvH2_gxorDsZTfJB8NqPCji-fNbDjWWou7U_jyEbN8XP-_jQDsD9EKdN4TIRRVZBj7G90")
                    .header("ennUnifiedCsrfToken", "00a9d9ac127f59d08b4778804598596b")
                    .execute()
                    .body();
            ObjectMapper objectMapper = new ObjectMapper();

            // 解析 JSON 字符串
            JsonNode rootNode = objectMapper.readTree(body);

            // 提取 data -> list 中的第一个对象
            JsonNode listNode = rootNode.path("data").path("list").get(0);

            if (Objects.isNull(listNode)) {
                System.out.println(appId);
                continue;
            }
            // 提取所需字段
            String name = listNode.path("name").asText();
            String projMgrName = listNode.path("projMgrName").asText();
            String code = listNode.path("code").asText();
            String groupIdStr = listNode.path("groupIdStr").asText();
            // 打印结果
//            System.out.printf("%s（名称：%s，负责人：%s，业务域：%s）", code, name, projMgrName, groupIdStr);
            String format = String.format("%s:%s:%s:%s", code, name, projMgrName, groupIdStr);
            list.add(format);
//            System.out.println();
        }
        Map<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(ths -> {
            String[] split = ths.split(":");
            return split[2];
        }, Collectors.mapping(ths -> {
            String[] split = ths.split(":");
            return String.format("%s（名称：%s，业务域：%s）", split[0], split[1], split[3]);
        }, Collectors.toList())));
        collect.forEach((key, value) -> {
            System.out.printf("%s(%d): %s",key, value.size(), value);
            System.out.println();
        });
    }

    @Test
    public void test() {

        List<String> list = Lists.newArrayList("000", "00d", "888");
        System.out.println(list.stream().collect(Collectors.partitioningBy(ths -> ths.contains("*"))));
    }


    public static class Result {


    }
}
